package client

import (
	"github.com/gin-gonic/gin"
	"likeadmin/admin/schemas/req"
	"likeadmin/admin/service/client"
	"likeadmin/config"
	"likeadmin/core"
	"likeadmin/core/request"
	"likeadmin/core/response"
	"likeadmin/middleware"
	"likeadmin/util"
)

var AccountGroup = core.Group("/client", newAccountHandler, regAccount, middleware.TokenAuth())

func newAccountHandler(srv client.IAccountService) *accountHandler {
	return &accountHandler{srv: srv}
}

func regAccount(rg *gin.RouterGroup, group *core.GroupBase) error {
	return group.Reg(func(handle *accountHandler) {
		rg.GET("/account/list", handle.list)
		rg.GET("/account/detail", handle.detail)
		rg.POST("/account/add", handle.add)
		rg.POST("/account/edit", handle.edit)
		rg.POST("/account/del", handle.del)
		rg.GET("/account/bearer_token", handle.bearerToken)
		rg.GET("/account/oppo_ad_data", handle.oppo_ad_data)
		rg.GET("/account/statistic", handle.statistic)
	})
}

type accountHandler struct {
	srv client.IAccountService
}

// list account列表
func (hd accountHandler) list(c *gin.Context) {
	var page request.PageReq
	var listReq req.AccountListReq
	if response.IsFailWithResp(c, util.VerifyUtil.VerifyQuery(c, &page)) {
		return
	}
	if response.IsFailWithResp(c, util.VerifyUtil.VerifyQuery(c, &listReq)) {
		return
	}
	res, err := hd.srv.List(c, page, listReq)
	response.CheckAndRespWithData(c, res, err)
}

// detail account详情
func (hd accountHandler) detail(c *gin.Context) {
	var detailReq req.AccountDetailReq
	if response.IsFailWithResp(c, util.VerifyUtil.VerifyQuery(c, &detailReq)) {
		return
	}
	res, err := hd.srv.Detail(detailReq.AccountId)
	response.CheckAndRespWithData(c, res, err)
}

// add account新增
func (hd accountHandler) add(c *gin.Context) {
	var addReq req.AccountAddReq
	if response.IsFailWithResp(c, util.VerifyUtil.VerifyBody(c, &addReq)) {
		return
	}
	response.CheckAndResp(c, hd.srv.Add(c, addReq))
}

// edit account编辑
func (hd accountHandler) edit(c *gin.Context) {
	var editReq req.AccountEditReq
	if response.IsFailWithResp(c, util.VerifyUtil.VerifyBody(c, &editReq)) {
		return
	}
	response.CheckAndResp(c, hd.srv.Edit(editReq))
}

// del account删除
func (hd accountHandler) del(c *gin.Context) {
	var delReq req.AccountDelReq
	if response.IsFailWithResp(c, util.VerifyUtil.VerifyBody(c, &delReq)) {
		return
	}
	response.CheckAndResp(c, hd.srv.Del(delReq.AccountId))
}

// bearerToken
func (hd accountHandler) bearerToken(c *gin.Context) {
	var req req.AccountBearerTokenReq
	if response.IsFailWithResp(c, util.VerifyUtil.VerifyBody(c, &req)) {
		return
	}
	response.CheckAndResp(c, hd.srv.BearerToken(req))
}

// del account删除
func (hd accountHandler) oppo_ad_data(c *gin.Context) {
	var delReq req.AccountDelReq
	if response.IsFailWithResp(c, util.VerifyUtil.VerifyBody(c, &delReq)) {
		return
	}
	response.CheckAndResp(c, hd.srv.OppoAdData(delReq.AccountId))
}

// list report列表
func (hd accountHandler) statistic(c *gin.Context) {

	var listReq req.AccountListReq
	if response.IsFailWithResp(c, util.VerifyUtil.VerifyBody(c, &listReq)) {
		return
	}
	adminId := config.AdminConfig.GetAdminId(c)
	res, err := hd.srv.Statistic(adminId, listReq)
	response.CheckAndRespWithData(c, res, err)
}
